package com.ims.controller;

import com.ims.entity.ClassInfoEntity;
import com.ims.entity.StudentInfoEntity;
import com.ims.form.InfoPandect.classcards.ClasscardsForm;
import com.ims.form.InfoPandect.numanalysis.NumanalysisForm;
import com.ims.form.InfoPandect.studentinfo.*;
import com.ims.form.InfoPandect.studentoptions.*;
import com.ims.repository.ClassInfoRepository;
import com.ims.repository.StudentInfoRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api
@RestController
public class InfoPandectController {

    @Autowired
    private ClassInfoRepository classInfoRepository;

    @Autowired
    private StudentInfoRepository studentInfoRepository;

    @PostMapping(value = "/numanalysis")
    public NumanalysisForm getNumanalysis(@RequestBody Map<String, Object> req){
        String shortName = (String)req.get("shortName");
        Map<String, Object> data = new HashMap<>();
        List<Long> sex = new ArrayList<>();
        sex.add(studentInfoRepository.findMenStudents(shortName));
        sex.add(studentInfoRepository.findWomenStudents(shortName));
        sex.add(0L);
        List<String> hometown = studentInfoRepository.findHometownByStuclass(shortName);
        List<Long> numberToNativePlace = new ArrayList<>();
        for(String home : hometown)
            numberToNativePlace.add(studentInfoRepository.findNumByStuclassAndHometown(shortName,home));
        data.put("sex", sex);
        data.put("hometown", hometown);
        data.put("numberToNativePlace", numberToNativePlace);
        return new NumanalysisForm(0,data);
    }

    @PostMapping(value = "/classcards")
    public ClasscardsForm getClasscards(@RequestBody Map<String, Object> req){
        Map<String, Object> data = new HashMap<>();
        List<ClassInfoEntity> classInfoEntities = classInfoRepository.findAll();
        List<String> _class = new ArrayList<>();
        List<String> major = new ArrayList<>();
        List<String> shortName = new ArrayList<>();
        for(ClassInfoEntity entity : classInfoEntities){
            _class.add(entity.getStu_class());
            major.add(entity.getMajor());
            shortName.add(entity.getShort_name());
        }
        data.put("_class", _class);
        data.put("major", major);
        data.put("shortName", shortName);
        return new ClasscardsForm(0, data);
    }

    @ApiOperation(value = "学生基本信息过滤器")
    @PostMapping(value = "/studentoptions")
    public StudentOptionsInfoForm getStudentOptions(@RequestBody @ApiParam(value = "切换显示“我分管的”和“所有学生”的开关值") getStudentOptionsForm req) {
        if(req.getSelectedMajor()==null)
            req.setSelectedMajor("%");
        if(req.getSelectedClass()==null)
            req.setSelectedClass("%");
        String Major = req.getSelectedMajor();
        String Stuclass = req.getSelectedClass();
        List<String> sex = studentInfoRepository.findSex(Major, Stuclass);
        List<String> nation = studentInfoRepository.findNation(Major, Stuclass);
        List<String> hometown = studentInfoRepository.findHometown(Major, Stuclass);
        List<String> dorm = studentInfoRepository.findDorm(Major, Stuclass);
        StudentOptions studentOptions = new StudentOptions(new ArrayList<String>(sex), new ArrayList<String>(nation), new ArrayList<String>(hometown), new ArrayList<String>(dorm));
        StudentOptionsChoiceForm studentOptionsChoiceForm = new StudentOptionsChoiceForm(studentOptions);
        StudentOptionsInfoForm studentOptionsInfoForm = new StudentOptionsInfoForm(0, "已获得性别、民族、籍贯、宿舍楼栋选项", studentOptionsChoiceForm);
        return studentOptionsInfoForm;
    }

    @ApiOperation(value = "学生信息接口", notes = "学生信息接口由班级成员的专业，班级，姓名，学号，手机号码，民族，籍贯，寝室组成")
    @PostMapping(value = "/studentinfo")
    public StudentInfoForm getStudentInfo(@RequestBody @ApiParam(value = "查找的学生实体.若 number 存在，其他非必选都不存在；同样地，若其他选项存在，number 一定不存在", required = true)
                                                  getStudentInfoForm req) {
        StudentInfoForm studentInfoForm;
        ArrayList<StudentForm> studentForms = new ArrayList<>();
        DataForm dataForm = new DataForm();
        if (req.getNumber() != null) {
            dataForm.setRecordQuantity(1L);
            StudentForm studentForm = new StudentForm();
            StudentInfoAdapter(studentForm, studentInfoRepository.findOne(req.getNumber()));
            studentForms.add(studentForm);
            dataForm.setStudents(studentForms);
            studentInfoForm = new StudentInfoForm(0, "ok", dataForm);
            return studentInfoForm;
        }
        StudentInfoEntity exmplent = new StudentInfoEntity();
        exmplent.setMajor(req.getMajor());
        exmplent.setStuclass(req.get_class());
        exmplent.setSex(req.getSex());
        exmplent.setNation(req.getNationality());
        exmplent.setHometown(req.getHometown());
        exmplent.setDorm(req.getBuilding());
        exmplent.setStuid(req.getNumber());
        Example<StudentInfoEntity> example = Example.of(exmplent);
        Sort sort = new Sort(Sort.Direction.ASC, "stuclass");
        Pageable pages = new PageRequest(req.getCurrentPage() - 1, req.getPageSize(), sort);
        Page<StudentInfoEntity> querylist = studentInfoRepository.findAll(example, pages);
        dataForm.setRecordQuantity(querylist.getTotalElements());
        List<StudentInfoEntity> studentInfoList = querylist.getContent();
        for (int i = 0; i < studentInfoList.size(); i++) {
            StudentForm studentForm = new StudentForm();
            StudentInfoAdapter(studentForm, studentInfoList.get(i));
            studentForms.add(studentForm);
        }
        dataForm.setStudents(studentForms);
        studentInfoForm = new StudentInfoForm(0, "ok", dataForm);
        return studentInfoForm;
    }

    static void StudentInfoAdapter(StudentForm studentForm, StudentInfoEntity studentInfoEntity) {
        studentForm.setMajor(studentInfoEntity.getMajor());
        studentForm.setName(studentInfoEntity.getStu_name());
        studentForm.set_class(studentInfoEntity.getStuclass());
        studentForm.setNumber(studentInfoEntity.getStuid());
        studentForm.setSex(studentInfoEntity.getSex());
        studentForm.setTel(studentInfoEntity.getPhone());
        studentForm.setNationality(studentInfoEntity.getNation());
        studentForm.setHometown(studentInfoEntity.getHometown());
        studentForm.setBuilding(studentInfoEntity.getDorm());
        studentForm.setDorm(studentInfoEntity.getDoor());
    }
}

